package com.yjd.bi.market.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.base.service.impl.BaseServiceImpl;
import com.yjd.comm.util.ArithUtil;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.NumberUtil;
import com.yjd.bi.market.model.MarketStatisticsModel;
import com.yjd.bi.market.service.IMarketStatisticsService;
import com.yjd.bi.report.enums.TimeType;
import com.yjd.bi.report.model.EChartsModel;
import com.yjd.bi.report.util.TimeDimensionUtil;
import com.yjd.bi.market.dao.IMarketStatisticsDao;

/**
 * <b>description</b>：资金量记录业务实现<br>
 * <b>time</b>：2016-05-25 10:23:24 <br>
 * <b>author</b>： chenpengfei@yijeidai.com
 */
@Service("marketStatisticsService")
public class MarketStatisticsServiceImpl extends BaseServiceImpl implements
		IMarketStatisticsService {

	/**
	 * 插入
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public MarketStatisticsModel insert(MarketStatisticsModel model)
			throws Exception {
		return this.marketStatisticsDao.insertModel(model, true);
	}

	/**
	 * 更新
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(MarketStatisticsModel model) throws Exception {
		return this.marketStatisticsDao.updateModel(model, true);
	}

	/**
	 * 根据id删除
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int delete(long id) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("id", id);
		return this.marketStatisticsDao.deleteModel(paramMap, true);
	}

	@Resource
	private IMarketStatisticsDao marketStatisticsDao;

	@Override
	public IBaseDao getBaseDao() {
		return this.marketStatisticsDao;
	}

	@SuppressWarnings("unchecked")
	@Override
	public EChartsModel getDates(TimeType timeType, Long timeFrom,
			String field, Long channel_id) throws Exception {
		// TODO Auto-generated method stub
		EChartsModel ec = new EChartsModel();
		ec.setxAxis(FrameUtil.newArrayList(null));
		ec.setLegend(FrameUtil.newArrayList(getFormateX(timeFrom)));
		ec.addSeries(FrameUtil.newArrayList(null), getFormateX(timeFrom));
		List<List<Long>> searchParam = TimeDimensionUtil.getSearchRanges(
				timeType, timeFrom);
		if (timeType==TimeType.DAY||field.equals("recharge_money")|| field.equals("invest_money")) {
			for (int i = searchParam.size() - 1; i >= 0; i--) {
				ec.getXAxis().add(
						DateUtil.getFormateStringByTimeDimention(searchParam
								.get(i).get(1), "yyyy-MM-dd"));
				Double sum = ArithUtil.round(this.marketStatisticsDao
						.getFundsSum(FrameUtil.newHashMap("field", field,
								"start", searchParam.get(i).get(0), "end",
								searchParam.get(i).get(1), "channel_id",
								channel_id), false), 2);
				ec.getSeries().get(0).getData()
						.add(NumberUtil.formatNumber(sum, NumberUtil.FORMAT_3));
			}
		} else {
			for (int i = searchParam.size() - 1; i >= 0; i--) {
				ec.getXAxis().add(
						DateUtil.getFormateStringByTimeDimention(searchParam
								.get(i).get(1), "yyyy-MM-dd"));
				// 注册用户数
				Double sum=this.marketStatisticsDao.getSummary(FrameUtil.newHashMap( "start",DateUtil.dateStringToTimestamp(searchParam
						.get(i).get(0).toString(), DateUtil.PATTERN_yyyyMMdd), "end",DateUtil.dateStringToTimestamp( searchParam.get(i).get(1).toString(), DateUtil.PATTERN_yyyyMMdd)+86399l,"propertyName", field,"channel_id",channel_id));
				ec.getSeries().get(0).getData().add(sum.toString());
			}
		}
		return ec;
	}

	public String getFormateX(Long value) {
		return DateUtil.timestampToDateString(DateUtil.dateStringToTimestamp(
				value + "", DateUtil.PATTERN_yyyyMMdd),
				DateUtil.PATTERN_yyyy_MM_dd);
	}

	@Override
	public Double getSumm(Map<String, Object> paramMap) throws Exception {
		// TODO Auto-generated method stub
		return this.marketStatisticsDao.getFundsSum(paramMap, true);
	}

	@Override
	public Integer recharge_account(Map<String, Object> paramMap)
			throws Exception {
		// TODO Auto-generated method stub
		return marketStatisticsDao.recharge_account(paramMap);
	}

	@Override
	public Integer register_account(Map<String, Object> paramMap)
			throws Exception {
		// TODO Auto-generated method stub
		return marketStatisticsDao.register_account(paramMap);
	}

	@Override
	public Integer invest_account(Map<String, Object> paramMap)
			throws Exception {
		// TODO Auto-generated method stub
		return marketStatisticsDao.invest_account(paramMap);
	}

	@Override
	public Double invest_price(Map<String, Object> paramMap) throws Exception {
		// TODO Auto-generated method stub
		return marketStatisticsDao.invest_price(paramMap);
	}

	@Override
	public Double recharge_price(Map<String, Object> paramMap) throws Exception {
		// TODO Auto-generated method stub
		return marketStatisticsDao.recharge_price(paramMap);
	}

}
